package com.zrt.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrt.springboot.controller.dto.UserDTO;
import com.zrt.springboot.entity.AlertData;
import com.zrt.springboot.entity.EmailAndSensorId;
import com.zrt.springboot.entity.River;
import com.zrt.springboot.entity.SensorRegistered;
import com.zrt.springboot.mapper.AlertDataMapper;
import com.zrt.springboot.mapper.SensorRegisteredMapper;
import com.zrt.springboot.service.CreateRiverDataService;
import com.zrt.springboot.utils.Simulated;
import com.zrt.springboot.utils.Threshold;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

@Service
public class CreateRiverDataServiceImpl implements CreateRiverDataService {

    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private SensorRegisteredMapper sensorRegisteredMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public boolean simulateDataBySensorId(String email,River river) throws InterruptedException {
        String context = null;

        double max = sensorRegisteredMapper.selectMax(river.getSensorId());
        double min = sensorRegisteredMapper.selectMin(river.getSensorId());

        if(river.getDepth() > max ){
            context = river.high() ;
            alertDataMapper.insert(new AlertData(0,river.getSensorId(), river.getDepth(), river.getTime(),0));
        }else if(river.getDepth() < min){
            context = river.low();
            alertDataMapper.insert(new AlertData(0,river.getSensorId(), river.getDepth(), river.getTime(),1));
        }
        if (context != null){
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("gy20_24jsjxynum1@163.com");
            message.setTo(email);
            message.setSubject("警报！！！");
            message.setText(context);
            javaMailSender.send(message);
        }
        return context != null ? true : false;
    }


}
